//
//  SecondViewController.m
//  UI07_相册滚动
//
//  Created by dllo on 16/6/14.
//  Copyright © 2016年 dllo. All rights reserved.
//
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#import "SecondViewController.h"

@interface SecondViewController ()<UIScrollViewDelegate>
@property (nonatomic,retain)UIScrollView *scrollView;
@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    self.navigationItem.title = [NSString stringWithFormat:@"第%ld张图片",_numPhotos];
    //点进来显示的那张图片
    [self createSubView];
    self.scrollView.contentOffset = CGPointMake(self.numPhotos * ScreenWidth, 0);
    
}

- (void)createSubView{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64)];
    self.scrollView.contentSize = CGSizeMake(ScreenWidth * (self.imageArray1.count + 2), ScreenHeight - 64);
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
     self.scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    for (NSInteger i = 1; i <= self.imageArray1.count; i++) {
        UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(ScreenWidth * i, 0, ScreenWidth, ScreenHeight - 64)];
        scroll.minimumZoomScale = 0.5;
        scroll.maximumZoomScale = 2;
        scroll.zoomScale = 1;
        scroll.delegate = self;
        [self.scrollView addSubview:scroll];
        [scroll release];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64)];
        imageView.userInteractionEnabled = YES;
        imageView.image = [self.imageArray1 objectAtIndex:i - 1];
        [scroll addSubview:imageView];
        [imageView release];
    }
    //在最前面加上一张最后一张图:
    UIScrollView *frontScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64)];
    frontScroll.contentSize = CGSizeMake(ScreenWidth, ScreenHeight - 64);
    [self.scrollView addSubview:frontScroll];
    [frontScroll release];
    UIImageView *frontImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64)];
    frontImageView.image = [self.imageArray1 lastObject];
    [frontScroll addSubview:frontImageView];
    [frontImageView release];
    
    //在最后面加上一张第一张图:
    UIScrollView *lastScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(ScreenWidth * (self.imageArray1.count + 1), 0, ScreenWidth, ScreenHeight - 64)];
    lastScroll.contentSize = CGSizeMake(ScreenWidth, ScreenHeight - 64);
    [self.scrollView addSubview:lastScroll];
    [lastScroll release];
    UIImageView *lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64)];
    lastImageView.image = [self.imageArray1 firstObject];
    [lastScroll addSubview:lastImageView];
    [lastImageView release];
    
    //初始化分页控件:
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(60, 500, 255, 30)];
    self.pageControl.numberOfPages = self.imageArray1.count;
    self.pageControl.currentPage = self.numPhotos - 1;
    self.pageControl.pageIndicatorTintColor = [UIColor redColor];
    [self.pageControl addTarget:self action:@selector(didClickedPageControl:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:_pageControl];
    [_pageControl release];
    
 
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return scrollView.subviews.firstObject;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    for (UIScrollView *scroll in scrollView.subviews) {
        if ([scroll isKindOfClass:[UIScrollView class]]) {
            scroll.zoomScale = 1;
        }
    }
    if (scrollView.contentOffset.x == 0) {
        self.scrollView.contentOffset = CGPointMake(self.imageArray1.count * ScreenWidth, 0);
    }
    if (scrollView.contentOffset.x == (self.imageArray1.count + 1 ) * ScreenWidth) {
        self.scrollView.contentOffset = CGPointMake(ScreenWidth, 0);
    }
    self.pageControl.currentPage = self.scrollView.contentOffset.x / ScreenWidth - 1;
  self.navigationItem.title = [NSString stringWithFormat:@"第%ld张图片", self.pageControl.currentPage + 1];

}
- (void)didClickedPageControl:(UIPageControl *)pageControl
{
    [self.scrollView setContentOffset:CGPointMake((pageControl.currentPage + 1) * ScreenWidth, 0) animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
